package com.smart.system.domain;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.smart.common.annotation.Excel;
import com.smart.common.core.domain.BaseEntity;

import java.util.List;

/**
 * 垃圾桶对象 bucket
 * 
 * @author smart
 * @date 2024-09-18
 */
@Data
@Builder
@TableName("bucket")
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
public class Bucket extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 主键标识 */
    @Schema(description ="${comment}")
    @TableField(value = "id",exist = true)
    @TableId(type = IdType.AUTO)
    private Integer id;

    /** 容量 */
    @Excel(name = "容量")
    @Schema(description ="容量")
    @TableField(value = "capacity",exist = true)
    private String capacity;

    /** 垃圾桶编码 */
    @Excel(name = "垃圾桶编码")
    @Schema(description ="垃圾桶编码")
    @TableField(value = "bucket_number",exist = true)
    private String bucketNumber;

    /** 名称 */
    @Excel(name = "名称")
    @Schema(description ="名称")
    @TableField(value = "name",exist = true)
    private String name;

    /** 已用容量 */
    @Excel(name = "阈值百分比")
    @Schema(description ="阈值百分比")
    @TableField(value = "threshold_value",exist = true)
    private double thresholdValue;

    /** 阈值百分比 */
    @Excel(name = "已用容量")
    @Schema(description ="已用容量")
    @TableField(value = "threshold",exist = true)
    private Integer threshold;

    /** 部门号 */
    @Excel(name = "部门号")
    @Schema(description ="部门号")
    @TableField(value = "dept_id",exist = true)
    private String deptId;

    /**已用容量*/
    @TableField(value = "usedCapacity",exist = false)
    @Schema(description ="已用容量2")
    private Integer usedCapacity;

    /**
     * 将4-19范围内的threshold值转换为百分比
     * 4对应0%，19对应100%
     *
     * @return 对应的百分比值(0-100)
     */
    public int convertThresholdToPercentage(Integer i) {
        // 限制输入值在4-19范围内
        int value = Math.max(4, Math.min(19, i));

        // 使用线性映射公式: (value - min) / (max - min) * 100
        return (int) Math.round((double) (value - 4) / (19 - 4) * 100);
    }
    @Excel(name = "类型0垃圾桶1垃圾箱")
    @Schema(description ="类型0垃圾桶1垃圾箱2垃圾站")
    @TableField(value = "type",exist = true)
    private Integer type;

    /** 乐观锁 */
    @Excel(name = "乐观锁")
    @Schema(description ="乐观锁")
    @TableField(value = "revision",exist = true)
    private String revision;

    /** 是否删除;0正常1删除 */
    @Schema(description ="乐观锁")
    @TableField(value = "del_flag",exist = true)
    private String delFlag;

    /** 是否启用;0正常1冻结 */
    @Excel(name = "是否启用;0正常1冻结")
    @Schema(description ="是否启用;0正常1冻结")
    @TableField(value = "status",exist = true)
    private String status;

    /** 位置JSON */
    @Excel(name = "位置json")
    @Schema(description ="不传/位置json")
    @TableField(value = "json_string",exist = true)
    private String jsonString;


    @Schema(description = "坐标经纬度")
    @TableField(exist = false)
    private AreaLocation areaLocation;
    public void setJsonString(String jsonString) {
        this.jsonString = jsonString;
        this.areaLocation = JSON.parseObject(jsonString, AreaLocation.class);
    }

    public void setAreaLocation(AreaLocation areaLocation) {
        this.areaLocation = areaLocation;
        this.jsonString = JSON.toJSONString(areaLocation);
    }
    public AreaLocation getAreaLocation(){
        return JSON.parseObject(jsonString, AreaLocation.class);
    }

    @Schema(description = "桶告警信息")
    @TableField(exist = true)
    private String alarm;

    @Schema(description = "垃圾箱设备号码")
    @TableField(exist = true)
    private String deviceNumber;


}
